#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# 检验版本号是否匹配

root_dir=$(cd `dirname $0`/.. && pwd -P)

# 获取开发工具版本号与MD5
VERSION_DATA=$( cat "$root_dir/conf/devtools_v" )
VERSION_DATA=(${VERSION_DATA//,/ })
TARGET_VERSION=${VERSION_DATA[0]}
TARGET_VERSION_MD5=${VERSION_DATA[1]}

# 检查snapcraft主版本号是否正确
SNAP_VERSION=$(grep -P "'\d+\.\d+\.\d{5,}" -o $root_dir/snap/snapcraft.yaml)
if [ "${SNAP_VERSION//\'/}" != "$TARGET_VERSION" ];then
    echo "SNAP版本不正确!!!"
    echo "SNAP版本: ${SNAP_VERSION//\'/} != 目标版本: $TARGET_VERSION"
    exit 1
fi

# 检查readme中的版本
README_VERSION=$(grep -P '[\-`]\d+\.\d+\.\d{5,}[\-`]' -o "$root_dir/readme.md")
for ver in $README_VERSION; do
    ver="${ver//[-\`]/}"
    if [ "${ver}" != "${TARGET_VERSION}" ];then
        echo "README中版本不正确!!!"
        echo "README中版本: ${ver} != 目标版本: ${TARGET_VERSION}"
        exit 1
    fi
done

# 检查snapcraft构建时下载的工具版本号是否正确
SNAP_DEVTOOL_VERSION=$(cat $root_dir/snap/snapcraft.yaml | grep -P "=\d+\d+\d{5,}" -o)
if [ "${SNAP_DEVTOOL_VERSION//=/}" != "${TARGET_VERSION//./}" ];then
    echo "SNAP工具版本不正确!!!"
    echo "snap下载版本: ${SNAP_DEVTOOL_VERSION//=/} != 目标版本: ${TARGET_VERSION//./}"
    exit 1
fi

# 有当前版本的TAG,检查snap版本是否符合要求
LATEST_TAG=$(git describe --abbrev=0)
if [[ $LATEST_TAG == "v${TARGET_VERSION}"* ]];then
    SNAP_VERSION=$(grep -P "'\d+\.\d+\.\d+-\d+" -o $root_dir/snap/snapcraft.yaml)
    if [ "v${SNAP_VERSION//\'/}" != "$LATEST_TAG" ];then
        echo "SNAP版本与TAG不符!!!"
        echo "SNAP版本: v${SNAP_VERSION//\'/} != 最新TAG: $LATEST_TAG"
        exit 1
    fi
fi
